import { getAllDicts } from '../common/js/request-dictionary-service.js';
export default {
  namespaced: true,
  state: {
    enterpriseConfig: null,
    allDict: {}
  },
  getters: {
    getDictByCodes: function (state) {
      return async function (dictCode) {
        if (!state.allDict[dictCode]) {
          let result = await getAllDicts();
          result.data.forEach(el => {
            if (!state.allDict[el.dictCode]) {
              state.allDict[el.dictCode] = [];
            }
            let ids = state.allDict[el.dictCode].map(el => el.id);
            if (!ids.includes(el.id)) {
              state.allDict[el.dictCode].push(el);
            }
          });
        };
        return state.allDict[dictCode];
      };
    }
  },
  mutations: {
    setEnterpriseConfig (state, config) {
      state.enterpriseConfig = config;
    }
  },
  actions: {}
};
